cc.Class({

    extends: cc.Component,

    onLoad() {
        window.const.init();
        this.initComponent();
        this.getWxUserInfo();
        this.wxInit();
    },

    onEnable: function () {
        this.addListener();
        this.showBannerAd();
    },
    onDisable: function () {
        this.removeListener();
        this.destroyBannerAd();
    },
    initComponent: function () {
        this.startBtn = cc.find("startBtn", this.node);
        this.btnRank = cc.find("btnRank", this.node);
        this.btnHelp = cc.find("btnHelp", this.node);
        this.btnShare = cc.find("btnShare", this.node);
        this.btnClose = cc.find("btnClose", this.node);
        this.DisplayNode = cc.find("Display", this.node);
        this.btnClose = cc.find("Display/btnClose", this.node);

        this.helpPanle = cc.find("helpPanle", this.node);
        this.btnCloseHelp = cc.find("helpPanle/btnCloseHelp", this.node);
        this.lbShouquan = cc.find("lbShouquan", this.node).getComponent(cc.Label);

        this.btnVideoAdTipOpen = cc.find("btnVideoAdTipOpen", this.node);

        this.videoTip = cc.find("videoTip", this.node);
        this.btnVideoAdSure = cc.find("videoTip/btnVideoAdSure", this.node);
        this.btnVideoAdClose = cc.find("videoTip/btnVideoAdClose", this.node);
    },
    addListener: function () {
        this.startBtnEvent = this.startBtn.on(cc.Node.EventType.TOUCH_END, this.onStartClick.bind(this));
        this.btnRankEvent = this.btnRank.on(cc.Node.EventType.TOUCH_END, this.onRankClick.bind(this));
        this.btnShareEvent = this.btnShare.on(cc.Node.EventType.TOUCH_END, this.onShareClick.bind(this));
        this.btnCloseEvent = this.btnClose.on(cc.Node.EventType.TOUCH_END, this.onCloseClick.bind(this));
        this.btnHelpEvent = this.btnHelp.on(cc.Node.EventType.TOUCH_END, this.onHelpClick.bind(this));
        this.btnCloseHelpEvent = this.btnCloseHelp.on(cc.Node.EventType.TOUCH_END, this.onCloseHelpClick.bind(this));
        this.btnVideoAdTipOpenEvent = this.btnVideoAdTipOpen.on(cc.Node.EventType.TOUCH_END, this.onVideoAdTipOpenClick.bind(this));

        this.btnVideoAdSureEvent = this.btnVideoAdSure.on(cc.Node.EventType.TOUCH_END, this.onVideoAdSureClick.bind(this));
        this.btnVideoAdCloseEvent = this.btnVideoAdClose.on(cc.Node.EventType.TOUCH_END, this.onVideoAdCloseClick.bind(this));
    },
    removeListener: function () {
        this.startBtn.off(cc.Node.EventType.TOUCH_END, this.startBtnEvent);
        this.btnRank.off(cc.Node.EventType.TOUCH_END, this.btnRankEvent);
        this.btnShare.off(cc.Node.EventType.TOUCH_END, this.btnShareEvent);
        this.btnClose.off(cc.Node.EventType.TOUCH_END, this.btnCloseEvent);
        this.btnHelp.off(cc.Node.EventType.TOUCH_END, this.btnHelpEvent);
        this.btnCloseHelp.off(cc.Node.EventType.TOUCH_END, this.btnCloseHelpEvent);
        this.btnVideoAdTipOpen.off(cc.Node.EventType.TOUCH_END, this.btnVideoAdTipOpenEvent);
        this.btnVideoAdSure.off(cc.Node.EventType.TOUCH_END, this.btnVideoAdSureEvent);
        this.btnVideoAdClose.off(cc.Node.EventType.TOUCH_END, this.btnVideoAdCloseEvent);
    },
    onStartClick: function () {
        window.UIManager.showWindow(window.GameConstant.LayerEnum.UI, window.GameConstant.PrefabNameEnum.GAME_TOUCH_WIND);
        window.UIManager.hideWindow(window.GameConstant.PrefabNameEnum.GAME_START_WIND);
    },
    onHelpClick: function () {
        this.helpPanle.active = true;
        this.destroyBannerAd();
    },
    onCloseHelpClick: function () {
        this.helpPanle.active = false;
        this.showBannerAd();
    },
    onVideoAdSureClick: function () {
        if (!CC_WECHATGAME) {
            console.log("视频广告");
            return;
        }
        window.ad.videoAdData.showRewardeVideo();
        this.videoTip.active = false;

    },
    onVideoAdCloseClick: function () {
        this.videoTip.active = false;

    },
    onVideoAdTipOpenClick: function () {
        this.videoTip.active = true;
    },
    onRankClick: function () {
        this.destroyBannerAd();
        this.DisplayNode.active = true;
        var msg = {
            messageType: window.GameConstant.WxMessageEnum.OPEN_RANK,
            MAIN_MENU_NUM: "x1"
        };
        window.WxServer.wxPostMessage(msg);
    },

    onShareClick: function () {
        // var title = "无聊英雄，你能比我更无聊吗?";
        var title = "一秒识图，来和我拼手速吧";
        var icon = "https://www.lizhaoblog.com/all-res/common/share.png";
        var query = "";
        window.WxServer.shareAppMessage(title, icon, query, function (res) {
            console.log("share success");
        });

    },
    onCloseClick: function () {
        this.showBannerAd();
        this.DisplayNode.active = false;
        var msg = {
            messageType: window.GameConstant.WxMessageEnum.CLOSE_RANK,
            MAIN_MENU_NUM: "x1"
        };
        window.WxServer.wxPostMessage(msg);
    },
    wxInit() {
        if (!CC_WECHATGAME) {
            console.log("初始化微信相关内容");
            return;
        }
        window.ad.videoAdData.init(window.common.wx.ad.videoFirstId);
        window.ad.videoAdData.registerVideoPlayComplete(this.videoSuccess.bind(this));
        window.ad.videoAdData.registerVideoPlayFail(this.videoFail.bind(this));
        window.ad.videoAdData.registerOnVideoHandler(this.VideoError.bind(this));
    },
    videoSuccess() {
        console.log("视频观看完成");
        window.touchViewData.hasFive = true;
    },
    videoFail() {

        console.log("videoFail");
        // this.VideoErr_lab.string = "观看完整广告才能获得奖励哦";
        // this.VideoErr.active = true;
        // this.scheduleOnce(function(){
        //     this.VideoErr.active = false;
        // },1)
    },
    VideoError() {
        console.log("VideoError");
        // this.VideoErr_lab.string = "今日奖励次数已经用完";
        // this.VideoErr.active = true;
        // this.scheduleOnce(function(){
        //     this.VideoErr.active = false;
        // },1)
    },

    getWxUserInfo() {

        var that = this;
        window.WxServer.wxLogin(function (res) {
            console.log("login success", res);
        });

        window.WxServer.wxGetSetting(
            function (res) {
                if (res.authSetting['scope.userInfo']) {
                    // 表示已经授权过了，可以直接wx.getUserInfo()获取用户信息
                    console.log("scope.userInfo is true");
                    window.WxServer.getUserInfo(function (res) {
                        console.log(res);
                    }
                    );

                } else {
                    var viewSize = cc.view.getFrameSize();
                    var vW = viewSize.width / 2;      //屏幕宽
                    var vH = viewSize.height / 2;

                    var w = that.startBtn.width * 8 * vW / 1920
                    var h = that.startBtn.height * 8 * vH / 1080

                    that.lbShouquan.node.active = true;
                    console.log("scope.userInfo is false");
                    window.WxServer.createUserInfoButton(
                        vW - w / 2,
                        vH - h / 2,
                        w,
                        h,
                        // window.const.wxconst.DEFAULT_CENTER_POINT_X,
                        // window.const.wxconst.DEFAULT_CENTER_POINT_Y,
                        // window.const.wxconst.DEFAULT_WIDTH,
                        // window.const.wxconst.DEFAULT_HEIGHT,
                        window.const.wxconst.DEFAULT_PIC,
                        function (res, wxBtn) {
                            that.lbShouquan.node.active = false;
                            console.log(res);
                            if (res.userInfo) {
                                wxBtn.destroy();
                                var userInfo = res.userInfo;
                                var nickName = userInfo.nickName;
                                var avatarUrl = userInfo.avatarUrl;
                                var gender = userInfo.gender; //性别 0：未知、1：男、2：女
                                var province = userInfo.province;
                                var city = userInfo.city;
                                var country = userInfo.country;

                                var geUser = {};
                                geUser.unick = userInfo.nickName;
                                geUser.usex = userInfo.gender;
                                geUser.uface = userInfo.avatarUrl;
                                geUser.province = userInfo.province;
                                geUser.city = userInfo.city;
                                geUser.country = userInfo.country;


                                wx.request({
                                    url: window.api.SAVE_USER,
                                    data: geUser,    //参数为键值对字符串
                                    method: "post",
                                    success: function (res) {
                                        console.log(res);
                                    }
                                });
                            }
                        });
                }

            }
        );
    },
    destroyBannerAd() {
        if (!CC_WECHATGAME) {
            console.log("销毁广告1");
            return;
        }
        if (window.ad.bannerFirst != null) {
            window.ad.bannerFirst.destroy();
        }
    },
    showBannerAd() {
        if (!CC_WECHATGAME) {
            console.log("显示广告1");
            return;
        }

        this.destroyBannerAd();
        var winSize = wx.getSystemInfoSync();
        var bannerHeight = 40;
        var bannerWidth = 300;
        window.ad.bannerFirst = wx.createBannerAd({
            adUnitId: window.common.wx.ad.bannerFirstId,
            style: {
                left:0,
                top: winSize.windowHeight - bannerHeight,
                width: bannerWidth
            }
        })
        window.ad.bannerFirst.show();
        window.ad.bannerFirst.onResize(res => {
            window.ad.bannerFirst.style.top = winSize.windowHeight - window.ad.bannerFirst.style.realHeight;
        })

        window.ad.bannerFirst.onError(function (err) {
            console.log(err);
        });
        window.ad.bannerFirst.onLoad(function () {
            console.log("banner 广告加载成功");
        });
    },
});
